package com.sskj.b70.p.activity;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.StockCoinBean;
import com.sskj.b70.m.bean.AppVersionBean;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.bean.PublicData;
import com.sskj.b70.m.bean.SocketSendBean;
import com.sskj.b70.m.bean.TransactionModel;
import com.sskj.b70.m.bean.UserInfo;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.m.http.WebSocketObserver;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.DisposUtil;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.v.activity.MainActivity;

import io.reactivex.disposables.Disposable;


/**
 * Created by Lee on 2018/1/25 0025.
 */

public class MainPresenter extends BasePresenter<MainActivity> {

    private WebSocketObserver stockSocket;
    private Disposable stockDisposable;
    private WebSocketObserver stockSocketFive;
    private Disposable stockDisposableFive;

    public void upDateUserInfo() {
        httpService.getUserInfo().execute(new JsonCallBack<HttpData<UserInfo>>(this, false) {
            @Override
            public void _onSuccess(Response<HttpData<UserInfo>> response) {
                HttpData<UserInfo> httpData = response.body();
                if (httpData.getStatus() == HttpConfig.STATUS_OK && httpData.getData() != null) {
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_IS_SHOP, httpData.getData().isIs_shop());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, httpData.getData().getStatus());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_PAY_SIZE, httpData.getData().getPay_list());
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_ACCOUNT, httpData.getData().getAccount());
                    if (httpData.getData().getRealname() != null) {
                        SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME, httpData.getData().getRealname());
                    }
                    if (httpData.getData().getIdcard() != null) {
                        SPUtils.put(MyAppLication.getApplication(), Constants.SP_IDCORD, httpData.getData().getIdcard());
                    }
                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_JGCODE, httpData.getData().getJigou_code());
//                    SPUtils.put(MyAppLication.getApplication(), Constants.SP_USDT, httpData.getData().getUsdt());

                    mView.updateUserInfo(httpData.getData());
                }
            }
        });
    }


    public void getNotice() {
        OkGo.<HttpData<PublicData>>post(HttpConfig.BASE_URL + HttpConfig.PUBLIC_DATA)
                .execute(new JsonCallBack<HttpData<PublicData>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<PublicData>> response) {
                        HttpData<PublicData> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.STATUS_OK) {
                            if (!TextUtils.isEmpty(httpData.getData().getTpgg())) {
                                mView.showNotice(httpData.getData().getTpgg());
                            }
                        }
                    }
                });

    }

    public void getVersion() {
        httpService.getVersion()
                .execute(new JsonCallBack<HttpData<AppVersionBean>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        mView.onVersionDataSuccess(httpData.getData());
                    }
                });

    }

    public void initWebSocket() {
        if (stockSocket == null) {
            stockSocket = httpService.pushCoinType(Constants.NO_FIVE_PUSH);
            stockDisposable = stockSocket
                    .map(s -> new Gson().fromJson(s, StockCoinBean.StockProductVOBean.class))
                    .subscribe(data -> RxBus.getDefault().post(data));
            stockSocketFive = httpService.pushCoinFive(Constants.FIVE_PUSH);
            stockDisposableFive = stockSocketFive
                    .map(s -> new Gson().fromJson(s, TransactionModel.class))
                    .subscribe(data -> RxBus.getDefault().post(data));
        } else {
            stockSocket.send(new Gson().toJson(new SocketSendBean(Constants.NO_FIVE_PUSH)));
            stockSocketFive.send(new Gson().toJson(new SocketSendBean(Constants.FIVE_PUSH)));
        }

    }

    public void closeWebSocket() {
        stockSocket = null;
        stockSocketFive = null;
        DisposUtil.close(stockDisposable);
        DisposUtil.close(stockDisposableFive);
    }

    @Override
    public void detachView() {
        super.detachView();
        closeWebSocket();
    }


}
